<?php
/**
 * @package mikrokosmos
 * mk html utilities file created on 4-nov-2004
 * jaco_at_pixeldump_dot_org
 */



// render a html page header
function get_page_header($title = "mikrokosmos", $pathPrepend = "") {

  $str = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n";
  $str .= "<html>\r\n\t<head>\r\n\t\t<title>";
  $str .= $title;
  $str .= "</title>\r\n\t";
  $str .= "<link rel=\"stylesheet\" href=\"";
  $str .= $pathPrepend .SERVER_MKCSS;
  $str .= "mk.css\" type=\"text/css\">";
  $str .= "\r\n\t<script language=\"JavaScript\" type=\"text/javascript\" src=\"";
  $str .= $pathPrepend .SERVER_MKJS;
  $str .= "mk_ajax.js\"></script>";
  $str .= "\r\n\t<script language=\"JavaScript\" type=\"text/javascript\" src=\"";
  $str .= $pathPrepend .SERVER_MKJS;
  $str .= "mk.js\"></script>\r\n</head>\r\n<body>\r\n";

  // stats
  if(PD_STATS){
  	$str .= "<script type=\"text/javascript\" src=\"http://www.pixeldump.org/pd_stats/php-stats.js.php\">";
  	$str .= "</script><noscript><img src=\"http://www.pixeldump.org/pd_stats/php-stats.php\" ";
    $str .= "border=\"0\" alt=\"\" title=\"\"/></noscript>";

    //define('__PHP_STATS_PATH__','/web/htdocs/www.pixeldump.org/home/pd_stats/');
    //include(__PHP_STATS_PATH__.'php-stats.redir.php');
  }
  //

  $str .= "<table align=\"center\" style=\"height: 100%\">\r\n<tr>\r\n<td valign=\"middle\">\r\n";

  return $str;
}


//
function get_swfchooser_blank_form($pg, $at = "", $swf = ""){

  $str = "<form name=\"swfchooser\" action=\"";
  $str .= $pg;
  $str .= "\" method=\"post\">\r\n<input type=\"hidden\" name=\"at\" value=\"";
  $str .= $at;
  $str .= "\">\r\n<input type=\"hidden\" name=\"swf\" value=\"";
  $str .= $swf;
  $str .= "\">\r\n</form>";

  return $str;
}

// close html page, adding "home" link
function get_page_ending($homeURL = "") {
  $str = "\r\n{ <a href=\"";
  $str .= $homeURL;
  $str .= "\">home</a> | <a href=\"docs/index.html\">mk documentation</a> }";
  $str .= "\r\n</td>\r\n</tr>\r\n</table>\r\n</body>\r\n</html>";

  return $str;
}


// close html page, adding a link bar
function get_page_ending2($blTxt, $blVal) {

  $str = "\r\n{";

  $cLink = count($blTxt);

  if($cLink){

  	for($i = 0; $i < $cLink; $i++){

  		$str .= " <a href=\"";
  		$str .= $blVal[$i];
  		$str .= "\">" .$blTxt[$i] ."</a> |";
  	}
  }

  $str .= " <a href=\"/mk/docs/index.html\">mk documentation</a> }";
  $str .= "\r\n</td>\r\n</tr>\r\n</table>\r\n</body>\r\n</html>";

  return $str;
}


// render a html table with swf header values
// note that it is intended to use in a form tag
function render_swfHeader_table($swfHeader){

  $str = "";
  $fSize = sprintf("%01.2f kb" ,$swfHeader["swfFileSize"] / 1024);

  $str .= "<table width=\"300\">";
  $str .= "<tr><td align=\"right\">width:</td><td><input type=\"text\" size=\"4\" maxlength=\"4\" name=\"sww\" value=\"";
  $str .= $swfHeader["stageWidth"];
  $str .= "\" onchange=\"hilite_swfHeader_bt_update()\"><input type=\"hidden\" name=\"swn\" value=\"";
  $str .= $swfHeader["swfFileName"];
  $str .= "\" onchange=\"hilite_swfHeader_bt_update()\"></td></tr>\r\n";
  $str .= "<tr><td align=\"right\">height:</td><td><input type=\"text\"  size=\"4\" maxlength=\"4\" name=\"swh\" value=\"";
  $str .= $swfHeader["stageHeight"];
  $str .= "\" onchange=\"hilite_swfHeader_bt_update()\"></td></tr>\r\n";
  $str .= "<tr><td align=\"right\">bgColor:</td><td><input type=\"text\"  size=\"7\" maxlength=\"7\" name=\"swbg\" value=\"";
  $str .= $swfHeader["backgroundColor"];
  $str .= "\" onchange=\"hilite_swfHeader_bt_update()\"></td></tr>\r\n";
  $str .= "<tr><td align=\"right\">fps:</td><td><input type=\"text\"  size=\"3\" maxlength=\"3\" name=\"fps\" value=\"";
  $str .= $swfHeader["fps"];
  $str .= "\" onchange=\"hilite_swfHeader_bt_update()\"></td></tr>\r\n";
  $str .= "<tr><td align=\"right\">frames:</td><td>";
  $str .= $swfHeader["frameCount"];
  $str .= "</td></tr>";
  $str .= "<tr><td align=\"right\">compressed:</td><td>";

  $isCmp = $swfHeader["isCompressed"] == "yes" ? array("checked", "") : array("", "checked");

  $str .= "\r\n&nbsp;yes <input type=\"radio\" name=\"cmp\" value=\"yes\" ";
  $str .= $isCmp[0];
  $str .= " onchange=\"hilite_swfHeader_bt_update()\">\r\n&nbsp;no <input type=\"radio\" name=\"cmp\" value=\"no\" ";
  $str .= $isCmp[1];

  $str .= " onchange=\"hilite_swfHeader_bt_update()\"></td></tr>";
  $str .= "<tr><td align=\"right\">version:</td><td>";
  $str .= $swfHeader["swfVersion"];
  $str .= "</td></tr>";
  $str .= "<tr><td align=\"right\">original file size:</td><td>";
  $str .= $fSize;
  $str .= "</td></tr>";
  $str .= "<tr><td></td><td><input type=\"button\" name=\"hbt\" value=\"update these values\"";
  $str .= " onMouseUp=\"submit_item_category('";
  $str .= SWFHEADER_CATEGORY;
  $str .= "')\"></td></tr>\r\n";
  $str .= "</table>";

  return $str;
}

/**
 * private:
 * build object and embed tag for swf
 * if stageWidth > 320 or stageHeight > 240 it show at 50%
 */
function render_swf_box($swfHeader = array(), $swfDir = "media/flash/", $scaleSWF = true){

  $maxWidth = 320;
  $maxHeight = 240;
  $swfFileName = $swfHeader["swfFileName"];
  $id = basename($swfFileName, ".swf");
  $sfPath = $swfDir .$swfFileName;

  if(isset($swfHeader["width"])) $swfHeader["stageWidth"] = $swfHeader["width"];
  if(isset($swfHeader["height"])) $swfHeader["stageHeight"] = $swfHeader["height"];

  $stageWidth = isset($swfHeader["stageWidth"]) ? $swfHeader["stageWidth"] : $maxWidth;
  $stageHeight = isset($swfHeader["stageHeight"]) ? $swfHeader["stageHeight"] : $maxHeight;

  $bgCol = isset($swfHeader["backgroundColor"]) ? $swfHeader["backgroundColor"] : "#FFFFFF" ;
  if(isset($swfHeader["bgcolor"])) $bgCol = $swfHeader["bgcolor"];

  if($scaleSWF && ($stageWidth > $maxWidth || $stageHeight > $maxHeight)){

	$xf = ceil($stageWidth / $maxWidth);
	$yf = ceil($stageHeight / $maxHeight);

	$scaleFactor = max($xf, $yf);
  	$stageWidth = round($stageWidth / $scaleFactor);
  	$stageHeight = round($stageHeight / $scaleFactor);
  }

  $swfAlign = isset($swfHeader["align"]) ? $swfHeader["align"] : "";
  $swfVersion = isset($swfHeader["version"]) ? $swfHeader["version"] : "8";
  $quality = isset($swfHeader["quality"]) ? $swfHeader["quality"] : "best";

  $str = "\r\n<object ";
  //$str .= " classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\"";
  //$str .= " ncodebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=";
  //$str .= $swfVersion";
  //$str .= ",0,0,0\"";
  $str .= " type=\"application/x-shockwave-flash\"";
  $str .= "\r\ndata=\"";
  $str .= $sfPath;
  $str .= "\"";
  $str .= "\r\nwidth=\"";
  $str .= $stageWidth;
  $str .= "\"\r\nhei" ."ght=\"";
  $str .= $stageHeight;
  $str .= "\"\r\nid=\"";
  $str .= $id;
  $str .= "\"\r\nalign=\"";
  $str .= $swfAlign;
  $str .= "\" allowScriptAccess=\"always\">";
  $str .= "\r\n\t<param name=\"movie\" value=\"";
  $str .= $sfPath;
  $str .= "\" />\r\n\t<param name=\"quality\" value=\"";
  $str .= $quality;
  $str .= "\" />\r\n\t<param name=\"bgcolor\" value=\"";
  $str .= $bgCol;
  $str .= "\" />";
  $str .= "\r\n\t<param name=\"allowScriptAccess\" value=\"always\" />";


  if(isset($swfHeader["wmode"])){
	$str .= "\r\n\t<param name=\"wmode\" value=\"";
	$str .= $swfHeader["wmode"];
	$str .= "\" />";
  }

  $str .= "\r\n\t<param name=\"allowScriptAccess\" value=\"sameDomain\" />";
  $str .= "\r\n\t<param name=\"swLiveConnect\" value=\"true\" />";

  //$str .= "\r\n\t\t<embed src=\"";
  //$str .= $sfPath;
  //$str .= "\"\r\n\t\tquality=\"high\"\r\n\t\t bgcolor=\"";
  //$str .= $bgCol;
  //$str .= "\"\r\n\t\twidth=\"";
  //$str .= $stageWidth;
  //$str .= "\"\r\n\t\thei" ."ght=\"";
  //$str .= $stageHeight;
  //$str .= "\"\r\n\t\tname=\"";
  //$str .= $id;
  //$str .= "\"\r\n\t\talign=\"middle\"";
  //$str .= "\r\n\t\tallowScriptAccess=\"sameDomain\"";
  //$str .= "\r\n\t\ttype=\"application/x-shockwave-flash\";";
  //$str .= "\r\n\t\tswLiveConnect=\"true\"";
  //$str .= "\r\n\t\tpluginspage=";
  //$str .= "\"http://www.macromedia.com/go/getflashplayer\" />\r\n";
  $str .= "\r\n</object>";

  return $str;
}




/**
 * private
 * render a blank div
 */
function render_blank_div($wd = 320, $hg = 240, $id = "blankdiv", $msg = ""){

	$str = "<div id=\"";
	$str .= $id;
	$str .= "\" style=\"width:";
	$str .= $wd;
	$str .= "px; height:";
	$str .= $hg;
	$str .= "px; background-color:#EEEEEE\">";

	if(strlen($msg)){

		$str .= "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" ";
		$str .= "align=\"center\" style=\"height:100%\">";
    	$str .= "\r\n<tr><td>";
		$str .= $msg;
		$str .= "</td></tr></table>";
	}

	$str .= "</div>";

	return $str;
}

function render_select_list($selectName, $itemAr, $defaultIndex = -1){

	$str = "<select name=\"";
	$str .= $selectName;
	$str .= "\">";

	$k = 0;
	$checked = "";

	foreach($itemAr as $v){
		$str .= "<option value=\"";
		$str .= $v;

		if($defaultIndex == $k) $checked = "checked";
		$str .= "\" " .$checked .">";
		$str .= $v;
		$str .= "</option>";

		$k++;
	}
	$str .= "</select>";

	return $str;
}

// render a part of swfwork containing hidden fields
function render_hidden_elements($swf, $opt){

    $str = "<input type=\"hidden\" name=\"swf\" value=\""; // swf we are working on
    $str .= $swf ."\">\r\n";

	/* old implementation
    $str .="<input type=\"hidden\" name=\"at\" value=\""; // action type
    $str .= $opt["at"] ."\">\r\n";

    $str .="<input type=\"hidden\" name=\"ic\" value=\""; // item category
    $str .= $opt["ic"]  ."\">\r\n";
    */

    foreach($opt as $k => $v){

	    $str .="<input type=\"hidden\" name=\"";
    	$str .= $k;
    	$str .= "\" value=\""; // action type
    	$str .= $v;
    	$str .= "\">\r\n";
    }

    return $str;
}


// build a html table with frame statistics
function render_frame_statistics($itemCountlist, $fn, $frameCount){

	$str= "";

	$tbl = new MKTable();
	$tbl->add_tableAttrib("WIDTH", 300);

	$row = array();
	$row[0]["cellContent"] = "<img src=\"imgs/icons/frames.gif\" width=\"25\" height=\"16\" alt=\"working frame\" title=\"working frame\">";
	$row[0]["cellAttribs"]["ALIGN"] = "center";
	$row[1]["cellContent"] = "<img src=\"imgs/icons/as.gif\" width=\"16\" height=\"16\" alt=\"actions\" title=\"actions\">";
	$row[1]["cellAttribs"]["ALIGN"] = "center";
	$row[2]["cellContent"] = "<img src=\"imgs/icons/shape.gif\" width=\"16\" height=\"16\" alt=\"shapes\" title=\"shapes\">";
	$row[2]["cellAttribs"]["ALIGN"] = "center";
	$row[3]["cellContent"] = "<img src=\"imgs/icons/mc.gif\" width=\"16\" height=\"16\" alt=\"movieclips\" title=\"movieclips\">";
	$row[3]["cellAttribs"]["ALIGN"] = "center";
	$row[4]["cellContent"] = "<img src=\"imgs/icons/button.gif\" width=\"16\" height=\"16\" alt=\"buttons\" title=\"buttons\">";
	$row[4]["cellAttribs"]["ALIGN"] = "center";
	$row[5]["cellContent"] = "<img src=\"imgs/icons/image.gif\" width=\"16\" height=\"16\" alt=\"images\" title=\"images\">";
	$row[5]["cellAttribs"]["ALIGN"] = "center";
	$row[6]["cellContent"] = "<img src=\"imgs/icons/video.gif\" width=\"16\" height=\"16\" alt=\"videos\" title=\"videos\">";
	$row[6]["cellAttribs"]["ALIGN"] = "center";
	$row[7]["cellContent"] = "<img src=\"imgs/icons/audio.gif\" width=\"16\" height=\"16\" alt=\"sounds\" title=\"sounds\">";
	$row[7]["cellAttribs"]["ALIGN"] = "center";
	$row[8]["cellContent"] = "<img src=\"imgs/icons/font.gif\" width=\"16\" height=\"16\" alt=\"fonts\" title=\"fonts\">";
	$row[8]["cellAttribs"]["ALIGN"] = "center";
	$row[9]["cellContent"] = "<img src=\"imgs/icons/raw.gif\" width=\"16\" height=\"16\" alt=\"other raw\" title=\"other raw tags\">";
	$row[9]["cellAttribs"]["ALIGN"] = "center";

	$tbl->add_row($row);
	$tbl->add_rowAttrib("CLASS", "bckgrid", 0);


	$row = array();
	$row[0]["cellAttribs"]["ALIGN"] = "center";

	if($frameCount == "1"){

		$s1 = "<input type=\"text\" size=\"3\" name=\"cfn\" onchange=\"check_maxFrameNumber(this)\" value=\"";
		$s1 .= $fn;
		$s1 .= "\" disabled>";
		$row[0]["cellContent"] = $s1;
	}
	else {

		$t1 = new MKTable();
		$r1 = array();
		$s1 = "<input type=\"text\" size=\"3\" name=\"cfn\" onchange=\"check_maxFrameNumber(this)\" value=\"";
	    $s1 .= $fn;
	    $s1 .= "\"";
		$r1[0]["cellContent"] = $s1;
		$r1[0]["cellAttribs"]["ALIGN"] = "center";
		$r1[0]["cellAttribs"]["ROWSPAN"] = 2;

		$s1 = "<a href=\"javascript:moveframe(true)\">";
		$s1 .= "<img src=\"imgs/icons/up_ar.gif\" width=\"8\" height=\"8\" border=\"0\"";
	    $s1 .= " alt=\"next frame\" title=\"next frame\"></a>";
		$r1[1]["cellContent"] = $s1;
		$t1->add_row($r1);

		$r1 = array();
		$s1 = "<a href=\"javascript:moveframe()\">";
	    $s1 .= "<img src=\"imgs/icons/dw_ar.gif\" width=\"8\" height=\"8\" border=\"0\"";
	    $s1 .= " alt=\"previous frame\" title=\"previous frame\">";
		$r1[0]["cellContent"] = $s1;

		$t1->add_row($r1);

		$row[0]["cellContent"] = $t1->fetch_html();
	}


	$itemList = array("actions", "shapes", "movieclips", "buttons", "images", "videos", "sounds", "fonts", "rawtags");
	$ics = array(ACTION_CATEGORY, SHAPE_CATEGORY, MOVIECLIP_CATEGORY, BUTTON_CATEGORY,
               IMAGE_CATEGORY, VIDEO_CATEGORY, SOUND_CATEGORY, FONT_CATEGORY, RAW_CATEGORY);

	$i = 1;
	foreach($itemList as $item){

		$s1 = "-";
		if($itemCountlist[$item]){

			$s1 = "<a href=\"javascript:submit_item_category(";
			$s1 .= $ics[($i - 1)];
			$s1 .= ")\">" .$itemCountlist[$item] ."</a>";
		}
		$row[$i]["cellContent"] = $s1;
		$row[$i]["cellAttribs"]["ALIGN"] = "center";
		$i++;
	}

  	$tbl->add_row($row);

	$str .= $tbl->fetch_html();

	return $str;
}



// build a html table with frame statistics
function render_frame_statistics2($swfHeader, $fn){

	$itemList = array("actions", "shapes", "movieclips", "buttons", "images", "videos", "sounds", "fonts", "rawtags");
	$ics = array(ACTION_CATEGORY, SHAPE_CATEGORY, MOVIECLIP_CATEGORY, BUTTON_CATEGORY,
               IMAGE_CATEGORY, VIDEO_CATEGORY, SOUND_CATEGORY, FONT_CATEGORY, RAW_CATEGORY);

	$spacers = array(25, 16, 16, 16, 16, 16, 16, 16, 16, 16);

	$iTotals = array();
	foreach($itemList as $item) $iTotals[$item] = 0;

	$str= "";

	$tbl = new MKTable();
	$tbl->add_tableAttrib("WIDTH", 300);

	$row = get_row_spaced($spacers);
	$tbl->add_row($row);

	$row = array();
	$row[0]["cellContent"] = "<img src=\"imgs/icons/frames.gif\" width=\"25\" height=\"16\" alt=\"working frame\" title=\"working frame\">";
	$row[0]["cellAttribs"]["ALIGN"] = "center";
	$row[1]["cellContent"] = "<img src=\"imgs/icons/as.gif\" width=\"16\" height=\"16\" alt=\"actions\" title=\"actions\">";
	$row[1]["cellAttribs"]["ALIGN"] = "center";
	$row[2]["cellContent"] = "<img src=\"imgs/icons/shape.gif\" width=\"16\" height=\"16\" alt=\"shapes\" title=\"shapes\">";
	$row[2]["cellAttribs"]["ALIGN"] = "center";
	$row[3]["cellContent"] = "<img src=\"imgs/icons/mc.gif\" width=\"16\" height=\"16\" alt=\"movieclips\" title=\"movieclips\">";
	$row[3]["cellAttribs"]["ALIGN"] = "center";
	$row[4]["cellContent"] = "<img src=\"imgs/icons/button.gif\" width=\"16\" height=\"16\" alt=\"buttons\" title=\"buttons\">";
	$row[4]["cellAttribs"]["ALIGN"] = "center";
	$row[5]["cellContent"] = "<img src=\"imgs/icons/image.gif\" width=\"16\" height=\"16\" alt=\"images\" title=\"images\">";
	$row[5]["cellAttribs"]["ALIGN"] = "center";
	$row[6]["cellContent"] = "<img src=\"imgs/icons/video.gif\" width=\"16\" height=\"16\" alt=\"videos\" title=\"videos\">";
	$row[6]["cellAttribs"]["ALIGN"] = "center";
	$row[7]["cellContent"] = "<img src=\"imgs/icons/audio.gif\" width=\"16\" height=\"16\" alt=\"sounds\" title=\"sounds\">";
	$row[7]["cellAttribs"]["ALIGN"] = "center";
	$row[8]["cellContent"] = "<img src=\"imgs/icons/font.gif\" width=\"16\" height=\"16\" alt=\"fonts\" title=\"fonts\">";
	$row[8]["cellAttribs"]["ALIGN"] = "center";
	$row[9]["cellContent"] = "<img src=\"imgs/icons/raw.gif\" width=\"16\" height=\"16\" alt=\"other raw\" title=\"other raw tags\">";
	$row[9]["cellAttribs"]["ALIGN"] = "center";

	$tbl->add_row($row);
	$tbl->add_rowAttrib("CLASS", "bckgrid", 1);

	$str = $tbl->fetch_html();
	$str .= "<div id=\"frameitems\">\r\n";

	$tbl = new MKTable();
	$tbl->add_tableAttrib("WIDTH", 300);
	$tbl->add_tableAttrib("id", "tblIM");

	$spacers = array(25, 16, 16, 16, 16, 16, 16, 16, 16);
	$row = get_row_spaced($spacers);
	$tbl->add_row($row);

	$frameCount = $swfHeader["frameCount"];

	for($j = 0; $j < $frameCount; $j++){

		$mkic = new MKSwfItemCounter($swfHeader["swfFileName"], $j + 1);
		$itemCountlist = $mkic->get_itemCountlist();

		$k = 0;
		foreach($itemList as $item){
			$iTotals[$item] += $itemCountlist[$item];
			$k += $itemCountlist[$item];
		}

		if(!$k) continue;
		$row = array();
		$row[0]["cellContent"] = $j + 1;
		$row[0]["cellAttribs"]["ALIGN"] = "center";

		$i = 1;
		foreach($itemList as $item){

			$s1 = "-";
			if($itemCountlist[$item]){

				$s1 = "<a href=\"javascript:submit_item_category(";
				$s1 .= $ics[($i - 1)];
				$s1 .= ")\">" .$itemCountlist[$item] ."</a>";
			}
			$row[$i]["cellContent"] = $s1;
			$row[$i]["cellAttribs"]["ALIGN"] = "center";

			$i++;
		}

	  	$tbl->add_row($row);

	  	$m = $tbl->get_rowCount() - 1;
	  	$tbl->add_rowAttrib("onmouseover",  "manage_tr(this, 'trhover')", $m);
	  	$tbl->add_rowAttrib("onmouseout",  "manage_tr(this, 'none')", $m);
	  	$s1 = "manage_tr(this, 'trselected', ";
	  	$s1 .= ($j + 1);
	  	$s1 .= ", 'set_frameNumber')";
	  	$tbl->add_rowAttrib("onmouseDown",  $s1, $m);
	}

	$str .= $tbl->fetch_html();
	$str .= "</div>\r\n";

	if($frameCount > 1){

		$tbl = new MKTable();
		$tbl->add_tableAttrib("WIDTH", 300);

		$row = get_row_spaced($spacers);
		$tbl->add_row($row);

		$row = array();
		$row[0]["cellContent"] = $frameCount;
		$row[0]["cellAttribs"]["ALIGN"] = "center";

		$k = 1;
		foreach($iTotals as $k => $v){
			$row[$k]["cellContent"] = $v;
			$row[$k]["cellAttribs"]["ALIGN"] = "center";
			$k++;
		}
		$tbl->add_row($row);
		$str .= $tbl->fetch_html();
	}

	return $str;
}

// support for render_frame_statistics
function render_swf_itemOption($itemCount, $itemOption){

  $str =  "<td align=\"center\">";
  $item = "-";

  if($itemCount){

    $str .= "<a href=\"javascript:submit_item_category(";
    $str .= $itemOption;
    $str .= ")\">" .$itemCount ."</a>";
  }
  else $str .= $item;

  $str .= "</td>\r\n";

  return $str;
}

/**
 * private: renders a tr table element containg as spacers as in spAr
 */
function render_tr_spacers($spAr){

	$str = "<tr>";
	foreach($spAr as $sp){
		$str .= "<td><img src=\"imgs/blend1px.gif\" width=\"";
		$str .= $sp;
		$str .= "\" height=\"1\"></td>";
	}

	return $str . "</tr>\r\n";
}

/**
 * private: render image box
 * if width > 320 or height > 240 renders at 50%
 */
function render_image_box($imageFileName, $imgWidth, $imgHeight){

	$wd = $imgWidth;
	$hg = $imgHeight;
	$imgShown = "100%";

	if($imgWidth > 640 || $imgHeight > 480){
		$wd = round($imgWidth / 4);
		$hg = round($imgHeight / 4);
		$imgShown = "25\%";
	}
	else if ($imgWidth > 320 || $imgHeight > 240){
		$wd = round($imgWidth / 2);
		$hg = round($imgHeight / 2);
		$imgShown = "50\%";
	}

	$str = "<table width=\"320\" height=\"240\"><tr><td align=\"center\"><div id=\"imgbox\">";
	$str .= "<image src=\"";
	$str .= SERVER_MKTMP .$imageFileName;
	$str .= "\" width=\"";
	$str .= $wd;
	$str .= "\" height=\"";
	$str .= $hg;
	$str .= "\" name=\"imgItem\" id=\"imgItem\"><div></td></tr>";
	$str .= "<tr><td>image zoom: ";
	$str .= $imgShown;
	$str .= "</td></tr></table>";
	//$str .= "";

	return $str;
}

/**
 * private: render js image list
 * based on an array with n images:
 * image file name,
 * image width
 * image height
 */
function get_js_image_list($imgAr){

	$str = "<scr" ."ipt language=\"Jav" ."aScr" ."ipt\" type=\"text/jav" ."ascr" ."ipt\">\r\n";
	$str .= "md.imgAr = new Array();\r\n";
	$str .= "var imgLength = 0;\r\n";

 	foreach($imgAr as $img){

		$str .= "md.imgAr[imgLength] = new Object();\r\n";
		$str .= "md.imgAr[imgLength][\"imgFN\"] = \"";
		$str .= $img["imgFN"];
		$str .= "\";\r\n";

		$str .= "md.imgAr[imgLength][\"imgWidth\"] = \"";
		$str .= $img["imgWidth"];
		$str .= "\";\r\n";

		$str .= "md.imgAr[imgLength][\"imgHeight\"] = \"";
		$str .= $img["imgHeight"];
		$str .= "\";\r\n";

		$str .= "imgLength++;\r\n";
	}

	$str .= "</scr" ."ipt>\r\n";

	return $str;
}


function get_js_swf_list($swfAr){

	$str = "\r\n<scr" ."ipt language=\"Jav" ."aScr" ."ipt\" type=\"text/jav" ."ascr" ."ipt\">\r\n";
	$str .= "md.swfAr = new Array(";

 	foreach($swfAr as $swf){

		$str .= "\"";
		$str .= $swf;
		$str .= "\",\r\n\t\t";
	}

	$str .= "\"\");\r\n";
	$str .= "</scr" ."ipt>\r\n";

	return $str;
}

/**
 * private: simple template implementations, not mine:
 * http://manlinux.com
 */
function get_template_contents($templateFile, $Ar){

  if(!file_exists($templateFile)) return;

  $templateContent = file_get_contents($templateFile);
  $keys = array_keys($Ar);

  for ($i = 0; $i < count($Ar); $i++){
    $templateContent = str_replace(strtolower('{' . $keys[$i]. '}'), $Ar[$keys[$i]], $templateContent);
  }

  return $templateContent;
}

/**
 * private:
 * format an html color string from an associative array
 * red, green blue
 */
function get_html_color($colorAr){
	$str = "#";

	$str .= substr("0" .dechex($colorAr["red"]), -2);
	$str .= substr("0" .dechex($colorAr["green"]), -2);
	$str .= substr("0" .dechex($colorAr["blue"]), -2);

	return strtoupper($str);

}

/**
 * private:
 * returns an array fit on mk_table (mk_table->add_row)
 * filled with image spacer transparent
 * useful to get spaced columns
 */
function get_row_spaced($spAr = array()){

	$row = array();
	$sCount = count($spAr);

	for($i = 0; $i < $sCount; $i++){

		$s1 = "<img src=\"blend1px.gif\" width=\"";
		$s1 .= $spAr[$i];
		$s1 .= "\" height=\"1\" />";
		$row[$i]["cellContent"] = $s1;
	}

	return $row;
}


/**
 * reads template file
 * returns content
 */
function get_template_content($tplFileName, $tplDir = SERVER_MKTPL){

	$tplPath = $tplDir .$tplFileName;
	$str = "";

	if(file_exists($tplPath)){
		$tpl = fopen($tplPath, "r");
		$str .= fread($tpl, filesize($tplPath));
		fclose($tpl);
	}

	return $str;
}

/**
 * render js swf_postload call
 * note that in the output html must be present swf_postload call
 */
function render_jsLoader($jsLoader = array()){


	$jslStr = "<script language=\"javascript\" type=\"text/javascript\">";
	$jslStr .= "\r\nwindow.onload=swfloader;";
	$jslStr .= "\r\n\r\nfunction swfloader(){";
	$jslStr .= "\r\n\tswf_postload('";
	$jslStr .= $jsLoader["divID"];
	$jslStr .= "',{";

	foreach($jsLoader as $k => $v){
		$jslStr.= $k .":'" .$v ."',";
	}

	$jslStr = substr($jslStr, 0, -1);
	$jslStr .= "});\r\n}\r\n</script>";

	return $jslStr;
}
?>